package com.shi.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.shi.annotation.Auth;
import com.shi.annotation.MyLog;
import com.shi.common.Result;
import com.shi.entity.po.Menu;
import com.shi.entity.vo.SessionAccountVO;
import com.shi.enums.AuthCodeEnum;
import com.shi.enums.LogType;
import com.shi.service.MenuService;
import com.shi.util.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * (Menu)表控制层
 *
 * @author makejava
 * @since 2024-05-14 15:23:11
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("menu")
public class MenuController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;


    /**
     * 通过菜单类型查询菜单
     *
     * @param menuType
     * @return
     */
    @ApiOperation("通过菜单类型查询菜单(查询所有目录)")
    @GetMapping("/menuType/{menuType}")
    @Auth(hasPerm = AuthCodeEnum.PERM_MENU_INSERT_DELETE_UPDATE)
    public Result getMenuByMenuType(@ApiParam(defaultValue = "M") @PathVariable("menuType") String menuType) {
        return Result.ok(menuService.queryMenuByMenuType(menuType));
    }

    /**
     * 查询所有菜单树
     *
     * @return
     */
    @ApiOperation("查询菜单树")
    @Auth(hasPerm = {AuthCodeEnum.PERM_MENU_SELECT,AuthCodeEnum.PERM_ROLE_GIVE_MENUS})
    @GetMapping("/all/tree")
    public Result getAllMenuTree() {
        return Result.ok(menuService.queryAllMenuTree());
    }

    /**
     * 通过角色id查询菜单id列表
     *
     * @param roleId
     * @return
     */
    @ApiOperation("通过角色id获取全选菜单(全选、半选)")
    @GetMapping("/roleId/{roleId}")
    @Auth(hasPerm = AuthCodeEnum.PERM_ROLE_GIVE_MENUS)
    public Result getFullCheckMenuIdByRoleId(@PathVariable("roleId") Integer roleId) {
        return Result.ok(menuService.queryFullCheckMenuIdByRoleId(roleId));
    }

    /**
     * 查询指定用户的路由信息
     *
     * @param accountId
     * @return
     */
    @ApiOperation("通过用户id获取路由树")
    @GetMapping("/routes/{accountId}")
    @Auth
    public Result getRoutesByAccountId(@PathVariable("accountId") Integer accountId, HttpSession session) {
        SessionAccountVO sessionAccountVO = UserContext.get();
        if (Boolean.TRUE.equals(sessionAccountVO.getIsAdmin())) {
            return Result.ok(menuService.queryAllRoutes());
        }
        return Result.ok(menuService.queryRoutesByAccountId(accountId));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过id查询菜单")
    @GetMapping("{id}")
    @Auth(hasPerm = AuthCodeEnum.PERM_MENU_SELECT)
    public Result queryById(@PathVariable("id") Integer id) {
        Menu menu = menuService.getById(id);
        return Result.ok(menu);
    }

    /**
     * 新增数据
     *
     * @param menu 实体
     * @return 新增结果
     */
    @ApiOperation("添加菜单")
    @ApiOperationSupport(ignoreParameters = "menuId")
    @PostMapping
    @Auth(hasPerm = AuthCodeEnum.PERM_MENU_INSERT_DELETE_UPDATE)
    @MyLog(type = LogType.INSERT)
    public Result add(@RequestBody Menu menu) {
        menuService.save(menu);
        return Result.ok();
    }

    /**
     * 编辑数据
     *
     * @param menu 实体
     * @return 编辑结果
     */
    @ApiOperation("通过id修改菜单")
    @PutMapping
    @Auth(hasPerm = AuthCodeEnum.PERM_MENU_INSERT_DELETE_UPDATE)
    @MyLog(type = LogType.UPDATE)
    public Result edit(@RequestBody Menu menu) {
        menuService.updateById(menu);
        return Result.ok();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation("通过id删除菜单")
    @DeleteMapping
    @Auth(hasPerm = AuthCodeEnum.PERM_MENU_INSERT_DELETE_UPDATE)
    @MyLog(type = LogType.DELETE)
    public Result deleteById(@RequestBody Integer id) {
        menuService.removeById(id);
        return Result.ok();
    }

    /**
     * 通过ids批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation("通过ids批量删除菜单")
    @DeleteMapping("/batch")
    @Auth(hasPerm = AuthCodeEnum.PERM_MENU_INSERT_DELETE_UPDATE)
    @MyLog(type = LogType.DELETE)
    public Result deleteBatchByIds(@RequestBody List<Integer> ids) {
        menuService.removeBatchByIds(ids);
        return Result.ok();
    }
}

